/**
 *  @file SDLObjectFactory.cpp
 *
 *  @date 5-dec-2009
 *  @author Ives van der Flaas
 */

#include <sstream>
#include "SDLObjectFactory.h"
#include "SDLEnemyShip.h"
#include "SDLFriendlyShip.h"
#include "SDLTextRepresentation.h"
#include "SDLImageRepresentation.h"
#include "SDLImageRepresentation.h"
#include "SDLLevelTitle.h"
#include "SDLScoreKeeper.h"
#include "linearController.h"
#include "representation.h"
#include "tinyxml.h"

void si_sdl::SDLObjectFactory::load()
{
	TiXmlDocument doc("config/sdldata.xml");
	doc.LoadFile();

	TiXmlDeclaration* decl = doc.FirstChild()->ToDeclaration();

	assert(std::string(decl->Version()) == std::string("1.0"));

	TiXmlElement* root = doc.FirstChildElement("sdldata");

	// Fill in ship's filenames
	{
		TiXmlElement* node = root->FirstChildElement("ship");

		while(node != 0)
		{
			std::string type = node->Attribute("type");
			assert(fShipFilenames.count(type) == 0);

			fShipFilenames[type] = node->GetText();

			assert(fShipFilenames.count(type) == 1);
			node = node->NextSiblingElement("ship");
		}
	}
}

std::tr1::shared_ptr<si::EnemyShip> si_sdl::SDLObjectFactory::getEnemyFighter(si::Vector2 pos, si::ShipDescr descr)
{
	assert(fShipFilenames.count(descr.getName()) == 1);

	std::tr1::shared_ptr<si::Representation> rep
				= std::tr1::shared_ptr<si::Representation>(
							new SDLImageRepresentation(fShipFilenames[descr.getName()]));


	return std::tr1::shared_ptr<si::EnemyShip>(new SDLEnemyShip(pos, rep, descr));
}

std::tr1::shared_ptr<si::LevelTitle> si_sdl::SDLObjectFactory::getLevelTitle(std::string title)
{
	return std::tr1::shared_ptr<si::LevelTitle>(
					new SDLLevelTitle(title));
}

std::tr1::shared_ptr<si::FriendlyShip> si_sdl::SDLObjectFactory::getFriendlyFighter()
{


	return std::tr1::shared_ptr<si::FriendlyShip>(new SDLFriendlyShip());
}


std::tr1::shared_ptr<si::Representation> si_sdl::SDLObjectFactory::getBackground(int level)
{
	// Only 4 backgrounds at the time, sorry!
	if(level > 4)
		level = level % 5 +1 ;


	std::stringstream ss;
	ss << level;
	std::string levelStr;
	ss >> levelStr;

	return std::tr1::shared_ptr<si::Representation>(
			new SDLImageRepresentation(
					std::string("images/bg")+
					levelStr+
					std::string(".png")));
}



